
package tankattack.world;

import gamepack.*;
import tankattack.sprites.*;
import java.awt.event.*;
import java.awt.*;
import java.util.Vector;
import java.util.Enumeration;

public class RoomB extends Room {

		static Resource r = ResourceLoader.getResource("tankattack.world.RoomB");
		public static RoomData rd = r.roomData.get("desert");
		public static Image roomImg = buildRoomImage(); // Use it this way if you need to build the roomImage on the fly.
		
		public static Image buildRoomImage() { // Note Image may be built on the fly, but the width and height are constants picked from config file
			// Build your image from ...whatever.
			if ( rd.img == Room.defaultImg ) throw new RuntimeException("RoomA - Unable to load " + rd.bkImageFile );
			return rd.img;
		}
		
	public void readRoomConfig() {
		TankAttackWorld CG = (TankAttackWorld) World.currentWorld;
		CG.player1Life = CG.player2Life = r.getValueAsInt("playerLife");
		CG.maxEnemies = r.getValueAsInt("maxEnemies");
		CG.enemySpawnTime = new TimeStamp( r.getValueAsInt("enemySpawnTime") );
		CG.nextRoom = r.getValue("nextRoom");
		CG.resetRoom = r.getValue("resetRoom");
	}
		
	
	public RoomB() { // Room Ctr should primarily create/load the room Image, otherwise default roomImage will be used.
	   super( roomImg, rd );  // For building roomImage dynamically, use static method to create the image and pass it to Room constructor
		readRoomConfig();  
		TankAttackWorld CG = (TankAttackWorld) World.currentWorld;		
		CG.initVars();
		NotificationManager.register( "P1_KILLED_SPRITE", CG );
		NotificationManager.register( "P2_KILLED_SPRITE", CG );
		
		// === Assumptions of RoomContents.txt ===
		// 1st Sprite is Player1, 2nd Sprite is Player2, 3rd Sprite is BASE Brick

		CG.rd = this.rd; 
		
		if (rd == null ) {
			throw new RuntimeException("Null roomdata for RoomA, check RoomA xml file");
		}
		if( rd.spawnStrings.length < 3 ) {
			throw new RuntimeException("Insufficient room data for RoomA, Atleast 3 spawn sprites should be present.");
		}
		
		// ---------------------------------------------------------------
		
		// ASSUMPTION - 
		// 1st Sprite is Player1
		// 2nd Sprite is Player 2
		// 3rd Sprite is Base Brick
		// 4th Sprite is Enemy Spawn Position1
		// 5th Sprite is Enemy Spawn Position2
		// 6th Sprite is Enemy Spawn Position3
		// 7th Sprite onwards is normal stage
		
		CG.player1 = Sprite.spawn( rd.spawnStrings[0] , null);
		CG.player2 = CG.is2PGame ? Sprite.spawn( rd.spawnStrings[1] , null) : null;

		// Remember where they are created so that you can spawn them at the same place again if they die
		CG.playerSpawnArea[0] = CG.player1.getBounds();
		CG.playerSpawnArea[1] = CG.is2PGame ? CG.player2.getBounds() : null;

		CG.base = Sprite.spawn( rd.spawnStrings[2] , null);
		
		// We are assuming the 3,4,5th sprite in the room tell us the position of the enemy spawns.		
		CG.dummyEnemyTank[0] = Sprite.getDummyInstance( rd.spawnStrings[3] );
		CG.dummyEnemyTank[1] = Sprite.getDummyInstance( rd.spawnStrings[4] );
		CG.dummyEnemyTank[2] = Sprite.getDummyInstance( rd.spawnStrings[5] );
		
			for( int i=6;i<rd.spawnStrings.length;i++) {
				Sprite.spawn( rd.spawnStrings[i] , null);
			}

		//--------------------------------------------------------------------------------------------------
        this.vp.s = CG.player1;

		//int t = 4,rx=0,ry=0,rw=getWidth()-1-t,rh=getHeight()-1-t;		// room wall dimensions
		
		// ---------------------------------------------------------------
		Application.addMouseListener(TankAttackWorld.ml);
		Game.println("Entered roomA()");
		//MusicEngine.play(CG._midi[1]);
		MusicEngine.play( r.music[0] );  // read from configuration file.
}

public void exit() {
		Environment.killSprites();
		Game.println("Exited roomA()");
		Application.removeMouseListener(TankAttackWorld.ml);
		TankAttackWorld CG = (TankAttackWorld) World.currentWorld;
		NotificationManager.unregister( "P1_KILLED_SPRITE", CG );
		NotificationManager.unregister( "P2_KILLED_SPRITE", CG );
}

public void render( Graphics g) {
	super.render(g,0,0);
	TankAttackWorld CG = (TankAttackWorld) World.currentWorld;
    if( CG.roomFinish != CG.NO_FINISH) {
        if( CG.roomFinish == CG.GOOD_FINISH ) {
				CG.displayScores(Game.gback);
        } else if(CG.roomFinish == CG.BAD_FINISH) {
          		CG.displayGameOver( Game.gback);
        }
    } else {
	   CG.displayLives( g );	
    }
}

public void update() {
       super.update();
	   TankAttackWorld CG = (TankAttackWorld) World.currentWorld;
	   CG.updateCurRoom();
    }
}